package com.zhaodm.practise.popupwindow;

import android.os.Bundle;
import android.app.Activity;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.widget.PopupWindow;

public class PopupWindowActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_popup_window);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_popup_window, menu);
        return true;
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    	switch (keyCode) {
    	case KeyEvent.KEYCODE_DPAD_CENTER:
    		showPopupWindow();
    		return true;
    	case KeyEvent.KEYCODE_BACK:
    		if (popup != null) {
    			popup.dismiss();
    		}
    		return true;
    	case KeyEvent.KEYCODE_DPAD_UP:
    		showPopupWindow();
    		return true;
    	case KeyEvent.KEYCODE_DPAD_DOWN:
    		if (popup != null) {
    			popup.dismiss();
    		}
    		return true;
    	}
    	return super.onKeyDown(keyCode, event);
    }
    
    private void showPopupWindow() {
    	if (popup == null) {
	        View root = this.getLayoutInflater().inflate(R.layout.play_ctrl_bar, null);
	        popup = new PopupWindow(root, 837, 108);
    	}
        
//    	popup.setAnimationStyle(R.style.PopupAnimation);
    	popup.setFocusable(false);
        popup.showAtLocation(findViewById(R.id.main), Gravity.BOTTOM|Gravity.CENTER, 0, 0);
    }
    
    private PopupWindow popup = null;
}
